package ua.tach.shortl.logic;

import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.math.Vector2;

public class RailRound extends Rail{
	public static final float SINGLEANGLE = 90f/(BASERAILSIZE*1.6f);
	public static final float TORADIAN = 3.1416f/180f;
	public RailRound(Switch left, Switch right, Vector2 sh, float r) {
		super(left, right, sh, r);
	}

	public RailRound(Point sh, float r, boolean paid) {
		super(sh, r, paid);
	}

	public Point getSecond() {
		switch((int)rotation) {
		case 0:
			return new Point(shift.x-1, shift.y+1);
		case 90:
			return new Point(shift.x+1, shift.y+1);
		case 180:
			return new Point(shift.x+1, shift.y-1);
		default:
			return new Point(shift.x-1, shift.y-1);
		}
	}

	public void render (GL10 gl) {
		super.render(gl, 0);
	}

	// For train moving
	public float getRailSize () {
		return BASERAILSIZE*1.6f;
	}

	public int getRailDir (Switch s) {
		return  (sw[0] == s)? 1 : 0 ;
	}

	public boolean isCorrectPos(RailPosition p) {
		return !(p.pos<0 || p.pos>= BASERAILSIZE*1.6f);
	}

	@Override
	public boolean getCoordinate(RailPosition pos) {
		RailRound.calcCoord(shift, rotation, pos);
		return true;
	}

	public static void calcCoord(Point pp, float a, RailPosition pos) {
		float angle = SINGLEANGLE * pos.pos;
		if(pos.dir) {
			pos.rotate = 180 + angle;
		} else {
			pos.rotate = 90 -angle;;
		}
		pos.rotate += a;

		float tsin = (float) Math.sin((angle)*TORADIAN);
		float tcos = (float) Math.cos((angle)*TORADIAN);

		switch((int)a) {
		case 0:
			pos.renderCoord.y = (float) (pp.y - ((pos.dir)?tcos-1:(tsin-1)));
			pos.renderCoord.x = (float) (pp.x - ((pos.dir)?tsin:(tcos)));
			break;
		case 90:
			pos.renderCoord.y = (float) (pp.y + ((pos.dir)?tsin:(tcos)));
			pos.renderCoord.x = (float) (pp.x - ((pos.dir)?(tcos-1):(tsin-1)));
			break;
		case 180:
			pos.renderCoord.y = (float) (pp.y-1 + ((pos.dir)?tcos:tsin));
			pos.renderCoord.x = (float) (pp.x + ((pos.dir)?tsin:tcos));
			break;
		default:
			pos.renderCoord.y = (float) (pp.y - ((pos.dir)?tsin:(tcos)));
			pos.renderCoord.x = (float) (pp.x-1 + ((pos.dir)?tcos:(tsin)));
		}
	}
}
